package willevaluate.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

import common.BaseActivity;
import common.Constant;
import utils.MyLogUtils;
import willevaluate.bean.ExpertEvaluateBean;
import willevaluate.model.QuestionnaireMajorModel;
import willevaluate.model.QuestionnaireModel;
import willevaluate.model.QuestionnaireModelBuilder;

/**
 * 问卷调查
 */

@ContentView(R.layout.activity_questionnaire)
public class Questionnaire extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.common_back)
    private ImageView common_back;
    @ViewInject(R.id.common_title)
    private TextView common_title;
    @ViewInject(R.id.questionnaire_item1)
    private RadioGroup questionnaire_item1;
    @ViewInject(R.id.questionnaire_item2)
    private RadioGroup questionnaire_item2;
    @ViewInject(R.id.questionnaire_item3)
    private RadioGroup questionnaire_item3;
    @ViewInject(R.id.questionnaire_item4)
    private RadioGroup questionnaire_item4;
    @ViewInject(R.id.questionnaire_item5)
    private RadioGroup questionnaire_item5;

    @ViewInject(R.id.questionnaire_item6)
    private RelativeLayout questionnaire_item6;
    @ViewInject(R.id.questionnaire_item7)
    private RelativeLayout questionnaire_item7;
    @ViewInject(R.id.questionnaire_item8)
    private RelativeLayout questionnaire_item8;
    @ViewInject(R.id.questionnaire_item9)
    private RelativeLayout questionnaire_item9;

    @ViewInject(R.id.questionnaire_item6_1)
    private TextView questionnaire_item6_1;
    @ViewInject(R.id.questionnaire_item7_1)
    private TextView questionnaire_item7_1;
    @ViewInject(R.id.questionnaire_item8_1)
    private TextView questionnaire_item8_1;
    @ViewInject(R.id.questionnaire_item9_1)
    private TextView questionnaire_item9_1;

    @ViewInject(R.id.questionnaire_item10)
    private EditText questionnaire_item10;
    @ViewInject(R.id.questionnaire_item11)
    private EditText questionnaire_item11;
    @ViewInject(R.id.questionnaire_item12)
    private EditText questionnaire_item12;
    @ViewInject(R.id.questionnaire_item13)
    private EditText questionnaire_item13;
    @ViewInject(R.id.questionnaire_item14)
    private EditText questionnaire_item14;
    @ViewInject(R.id.questionnaire_item15)
    private Button questionnaire_item15;
    private QuestionnaireModelBuilder questionnaireModelBuilder;
    private QuestionnaireModel questionnaireModel;
    private MyHandler1 myHandler1;
    private String orderid;
    private String subject;//上个页面传过来的文科理科
    private final int REQUESTCODE_PROV = 1;
    private final int REQUESTCODE_SUBJECT = 2;
    private final int REQUESTCODE_STU_INTENT = 3;
    private final int REQUESTCODE_PAR_INTENT = 4;

    private ExpertEvaluateBean expertEvaluateBean;

    private String prov = "";//想去的省份
    private String adv_subject = "";//擅长科目
    private String stu_intent = "";//学生志愿
    private ArrayList<QuestionnaireMajorModel> stu_majorsList = new ArrayList<>();
    private String par_intent = "";//家长志愿
    private ArrayList<QuestionnaireMajorModel> par_majorsList = new ArrayList<>();


    private static class MyHandler1 extends Handler {
        WeakReference<Questionnaire> activityWeakReference;

        public MyHandler1(Activity context) {
            activityWeakReference = new WeakReference<>((Questionnaire) context);
        }

        @Override
        public void handleMessage(Message msg) {
            final Questionnaire activity = activityWeakReference.get();
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS://成功返回
                    try {
                        JSONObject jsonObject = new JSONObject(String.valueOf(msg.obj));
                        int err_code = jsonObject.getInt("err_code");
                        if (err_code == 0) {
                            Intent i = new Intent(activity, ExpertEvaluateCompleteCollege.class);
                            i.putExtra("orderid", activity.orderid);
                            i.putExtra("expertEvaluateBean",activity.expertEvaluateBean);
                            activity.startActivity(i);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityController.addActivity(this);
        orderid = getIntent().getStringExtra("orderid");
        subject = getIntent().getStringExtra("subject");
        expertEvaluateBean = (ExpertEvaluateBean) getIntent().getSerializableExtra("expertEvaluateBean");
        myHandler1 = new MyHandler1(this);
        questionnaireModelBuilder = new QuestionnaireModelBuilder();
        initView();
    }


    private void initView() {
        common_back.setOnClickListener(this);
        questionnaire_item15.setOnClickListener(this);
        common_title.setText("问卷调查");
        if (expertEvaluateBean != null) {
            ExpertEvaluateBean.QuestionDetailsBean questionDetails = expertEvaluateBean.getQuestionDetails();
            if (questionDetails != null) {
                if (!TextUtils.isEmpty(questionDetails.getIs_pro())) {
                    questionnaireModelBuilder.setIs_pro(questionDetails.getIs_pro());
                    if ("是".equals(questionDetails.getIs_pro())) {
                        ((RadioButton) findViewById(R.id.questionnaire_item1_1)).setChecked(true);
                    } else {
                        ((RadioButton) findViewById(R.id.questionnaire_item1_2)).setChecked(true);

                    }
                }
                if (!TextUtils.isEmpty(questionDetails.getIs_advance())) {
                    questionnaireModelBuilder.setIs_advance(questionDetails.getIs_advance());
                    if ("是".equals(questionDetails.getIs_advance())) {
                        ((RadioButton) findViewById(R.id.questionnaire_item2_1)).setChecked(true);
                    } else {
                        ((RadioButton) findViewById(R.id.questionnaire_item2_2)).setChecked(true);

                    }
                }
                if (!TextUtils.isEmpty(questionDetails.getIs_extraexam())) {
                    questionnaireModelBuilder.setIs_extraexam(questionDetails.getIs_extraexam());
                    if ("是".equals(questionDetails.getIs_extraexam())) {
                        ((RadioButton) findViewById(R.id.questionnaire_item3_1)).setChecked(true);
                    } else {
                        ((RadioButton) findViewById(R.id.questionnaire_item3_2)).setChecked(true);

                    }
                }
                if (!TextUtils.isEmpty(questionDetails.getIs_plan())) {
                    questionnaireModelBuilder.setIs_plan(questionDetails.getIs_plan());
                    switch (questionDetails.getIs_plan()) {
                        case "考研":
                            ((RadioButton) findViewById(R.id.questionnaire_item4_1)).setChecked(true);
                            break;
                        case "出国":
                            ((RadioButton) findViewById(R.id.questionnaire_item4_2)).setChecked(true);
                            break;
                        case "就业":
                            ((RadioButton) findViewById(R.id.questionnaire_item4_3)).setChecked(true);
                            break;
                        case "其他":
                            ((RadioButton) findViewById(R.id.questionnaire_item4_4)).setChecked(true);
                            break;
                        default:
                            break;
                    }
                }
                if (!TextUtils.isEmpty(questionDetails.getBody())) {
                    questionnaireModelBuilder.setBody(questionDetails.getBody());
                    switch (questionDetails.getBody()) {
                        case "较弱":
                            ((RadioButton) findViewById(R.id.questionnaire_item5_1)).setChecked(true);
                            break;
                        case "一般":
                            ((RadioButton) findViewById(R.id.questionnaire_item5_2)).setChecked(true);
                            break;
                        case "良好":
                            ((RadioButton) findViewById(R.id.questionnaire_item5_3)).setChecked(true);
                            break;
                        default:
                            break;
                    }
                }
                if (!TextUtils.isEmpty(questionDetails.getWantedcity())) {
                    questionnaireModelBuilder.setWantedcity(questionDetails.getWantedcity());
                    prov = questionDetails.getWantedcity();
                    questionnaire_item6_1.setText(questionDetails.getWantedcity());
                }
                if (!TextUtils.isEmpty(questionDetails.getAdv_subject())) {
                    questionnaireModelBuilder.setAdv_subject(questionDetails.getAdv_subject());
                    adv_subject = questionDetails.getAdv_subject();
                    questionnaire_item7_1.setText(questionDetails.getAdv_subject());
                }
                if (!TextUtils.isEmpty(questionDetails.getSt_intention())) {
                    questionnaireModelBuilder.setSt_intention(questionDetails.getSt_intention());
                    stu_intent = questionDetails.getSt_intention();
                    questionnaire_item8_1.setText(questionDetails.getSt_intention());
                }
                if (!TextUtils.isEmpty(questionDetails.getPra_intention())) {
                    questionnaireModelBuilder.setPra_intention(questionDetails.getPra_intention());
                    par_intent = questionDetails.getPra_intention();
                    questionnaire_item9_1.setText(questionDetails.getPra_intention());
                }
                if (!TextUtils.isEmpty(questionDetails.getSt_special())) {
                    questionnaireModelBuilder.setSt_special(questionDetails.getSt_special());
                    questionnaire_item10.setText(questionDetails.getSt_special());
                }
                if (!TextUtils.isEmpty(questionDetails.getSt_hobby())) {
                    questionnaireModelBuilder.setSt_hobby(questionDetails.getSt_hobby());
                    questionnaire_item11.setText(questionDetails.getSt_hobby());
                }
                if (!TextUtils.isEmpty(questionDetails.getPrajob())) {
                    questionnaireModelBuilder.setPrajob(questionDetails.getPrajob());
                    questionnaire_item12.setText(questionDetails.getPrajob());
                }
                if (!TextUtils.isEmpty(questionDetails.getFa_bg())) {
                    questionnaireModelBuilder.setFa_bg(questionDetails.getFa_bg());
                    questionnaire_item13.setText(questionDetails.getFa_bg());
                }
                if (!TextUtils.isEmpty(questionDetails.getRemark())) {
                    questionnaireModelBuilder.setRemark(questionDetails.getRemark());
                    questionnaire_item14.setText(questionDetails.getRemark());
                }

            }
        }
        questionnaire_item1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.questionnaire_item1_1:
                        questionnaireModelBuilder.setIs_pro("是");
                        break;
                    case R.id.questionnaire_item1_2:
                        questionnaireModelBuilder.setIs_pro("否");
                        break;
                    default:
                        break;
                }
            }
        });
        questionnaire_item2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.questionnaire_item2_1:
                        questionnaireModelBuilder.setIs_advance("是");
                        break;
                    case R.id.questionnaire_item2_2:
                        questionnaireModelBuilder.setIs_advance("否");
                        break;
                    default:
                        break;
                }
            }
        });
        questionnaire_item3.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.questionnaire_item3_1:
                        questionnaireModelBuilder.setIs_extraexam("是");
                        break;
                    case R.id.questionnaire_item3_2:
                        questionnaireModelBuilder.setIs_extraexam("否");
                        break;
                    default:
                        break;
                }
            }
        });
        questionnaire_item4.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.questionnaire_item4_1:
                        questionnaireModelBuilder.setIs_plan("考研");
                        break;
                    case R.id.questionnaire_item4_2:
                        questionnaireModelBuilder.setIs_plan("出国");
                        break;
                    case R.id.questionnaire_item4_3:
                        questionnaireModelBuilder.setIs_plan("就业");
                        break;
                    case R.id.questionnaire_item4_4:
                        questionnaireModelBuilder.setIs_plan("其他");
                        break;
                    default:
                        break;
                }
            }
        });
        questionnaire_item5.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.questionnaire_item5_1:
                        questionnaireModelBuilder.setBody("较弱");
                        break;
                    case R.id.questionnaire_item5_2:
                        questionnaireModelBuilder.setBody("一般");
                        break;
                    case R.id.questionnaire_item5_3:
                        questionnaireModelBuilder.setBody("良好");
                        break;
                    default:
                        break;
                }
            }
        });
        questionnaire_item6.setOnClickListener(this);
        questionnaire_item7.setOnClickListener(this);
        questionnaire_item8.setOnClickListener(this);
        questionnaire_item9.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.common_back:
                Back();
                break;
            case R.id.questionnaire_item6:
                Intent i = new Intent(this, QuestionnaireProv_Subject.class);
                i.putExtra("prov", prov);
                startActivityForResult(i, REQUESTCODE_PROV);
                break;
            case R.id.questionnaire_item7:
                Intent i1 = new Intent(this, QuestionnaireProv_Subject.class);
                i1.putExtra("subject", subject);
                i1.putExtra("adv_subject", adv_subject);
                startActivityForResult(i1, REQUESTCODE_SUBJECT);
                break;
            case R.id.questionnaire_item8:
                Intent i2 = new Intent(this, QuestionnaireMajor.class);
                i2.putExtra("majorsList", stu_majorsList);
                startActivityForResult(i2, REQUESTCODE_STU_INTENT);
                break;
            case R.id.questionnaire_item9:
                Intent i3 = new Intent(this, QuestionnaireMajor.class);
                i3.putExtra("majorsList", par_majorsList);
                startActivityForResult(i3, REQUESTCODE_PAR_INTENT);
                break;
            case R.id.questionnaire_item15:
                //提交
                questionnaireModelBuilder.setWantedcity(prov);
                questionnaireModelBuilder.setAdv_subject(questionnaire_item7_1.getText().toString());
                questionnaireModelBuilder.setSt_intention(stu_intent);
                questionnaireModelBuilder.setPra_intention(par_intent);
                questionnaireModelBuilder.setSt_special(questionnaire_item10.getText().toString());
                questionnaireModelBuilder.setSt_hobby(questionnaire_item11.getText().toString());
                questionnaireModelBuilder.setPrajob(questionnaire_item12.getText().toString());
                questionnaireModelBuilder.setFa_bg(questionnaire_item13.getText().toString());
                questionnaireModelBuilder.setRemark(questionnaire_item14.getText().toString());
                questionnaireModelBuilder.setOrderid(orderid);
                questionnaireModel = questionnaireModelBuilder.createQuestionnaireModel();
                questionnaireModel.subimt(this, myHandler1);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_CANCELED) {
            switch (requestCode) {
                case REQUESTCODE_PROV:
                    prov = data.getStringExtra("prov");
                    questionnaire_item6_1.setText(prov);
                    break;
                case REQUESTCODE_SUBJECT:
                    adv_subject = data.getStringExtra("subject");
                    questionnaire_item7_1.setText(adv_subject);
                    break;
                case REQUESTCODE_STU_INTENT:
                    stu_intent = data.getStringExtra("majors");
                    questionnaire_item8_1.setText(stu_intent);
                    stu_majorsList = (ArrayList<QuestionnaireMajorModel>) data.getSerializableExtra("majorsList");
                    break;
                case REQUESTCODE_PAR_INTENT:
                    par_intent = data.getStringExtra("majors");
                    questionnaire_item9_1.setText(par_intent);
                    par_majorsList = (ArrayList<QuestionnaireMajorModel>) data.getSerializableExtra("majorsList");
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void onBackPressed() {
        Back();
    }
    private void Back(){
        this.finish();
    }
}
